<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="图片" :label-width="formLabelWidth" prop="imgurl">
          <el-upload
            class="avatar-uploader"
            action="https://tfnfw.tfnhg.com/oss_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.imgurl" :src="form.imgurl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>

        </el-form-item>
        <el-form-item label="权值" :label-width="formLabelWidth" prop="qz">
          <el-input v-model="form.qz" type="number" />
        </el-form-item>
        <el-form-item label="调转路径" :label-width="formLabelWidth" prop="url">
          <el-input v-model="form.url" type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFun('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { uploadBannerApi } from '@/api/main'
export default {
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      form: {
        id: '',
        imgurl: '',
        qz: '',
        url: ''
      },
      rules: {
        imgurl: [
          { required: true, message: '请选择图片', trigger: 'change' }
        ],
        qz: [
          { required: true, message: '请输入权值', trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(res, file)
      this.form.imgurl = res
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type.split('/')[0] === 'image'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片文件!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    submitFun(formname) {
      this.$refs[formname].validate((valid) => {
        if (valid) {
          const data = {
            id: this.form.id,
            imgurl: this.form.imgurl,
            qz: this.form.qz,
            url: this.form.url
          }
          console.log(data)
          uploadBannerApi(data).then(res => {
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.$emit('reloadFun')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .red {
    color: red;
  }
  .span {
    color: #ccc;
    font-size: 12px;
  }
</style>
